package Server;

import Data.FriendListData;
import Data.PrivateInformationData;
import Data.event;
import MyInterfaces.MassageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

public class ExitLogin {

    private FriendListData friendListData;

    public void exit() throws IOException {
        String id=friendListData.getMydata().getUserid();
        PrivateInformationData mydata=friendListData.getMydata();
        mydata.setIsonline(0);
        ArrayList<PrivateInformationData> list=friendListData.getFriendList();

        for (int i = 0; i < list.size(); i++) {
            if(ManageClientConnectionServerThread.existId(list.get(i).getUserid()))
            {
                //如果这个人在线，需要修改在线信息
                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(list.get(i).getUserid());

                event returnmassage=new event();
                returnmassage.setType(MassageType.Change_Is_Online);
                returnmassage.setT(mydata);

                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                writer.writeObject(returnmassage);
                writer.flush();
            }
        }
        ManageClientConnectionServerThread.deleteSocket(id);
    }

    public ExitLogin(FriendListData friendListData) {
        this.friendListData = friendListData;
    }

    public FriendListData getFriendListData() {
        return friendListData;
    }

    public void setFriendListData(FriendListData friendListData) {
        this.friendListData = friendListData;
    }
}
